package cn.gov.customs.h2018.controller;

import java.util.List;
import javax.annotation.Resource;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.google.common.collect.Lists;
import cn.gov.customs.h2018.pojo.Article;
import cn.gov.customs.h2018.service.ParallelQuery;
import lombok.extern.slf4j.Slf4j;

@RestController
@Slf4j
public class Query {

  private final int loopCount = 1; // 循环次数
  private final int articleCount = 20; // 每次获取的文章数

  @Resource(name="LzhpQueryImpl")
  ParallelQuery queryLzhp;

  @Resource(name="Lzhp2QueryImpl")
  ParallelQuery queryLzhp2;

  @Resource(name="zjdQueryImpl")
  ParallelQuery asyncQuery;

  @GetMapping("/test")
  public List<String> query() {

    StopWatch watch = new StopWatch();
    List<String> result = Lists.newArrayList();

    log.info("lzhp begin-------------");
    watch.start();
    List<Article> resultArticles = Lists.newArrayList();
    for (int i = 0; i < loopCount; i++) {
      resultArticles = queryLzhp.query(articleCount);
    }
    watch.stop();
    result.add( String.format("lzhp time: %d ms , %s", watch.getTotalTimeMillis(), resultArticles));
    log.info("lzhp end ---------");
    
    
    log.info("lzhp2 begin-------------");
    watch.start();
    resultArticles = Lists.newArrayList();
    for (int i = 0; i < loopCount; i++) {
      resultArticles = queryLzhp2.query(articleCount);
    }
    watch.stop();
    result.add( String.format("lzhp2 time: %d ms , %s", watch.getTotalTimeMillis(), resultArticles));
    log.info("lzhp2 end ---------");

    return result;
  }

  @GetMapping("/test2")
  public List<String> query2() {

    StopWatch watch = new StopWatch();
    List<String> result = Lists.newArrayList();

    watch.start();
    List<Article> resultArticles = Lists.newArrayList();
    for (int i = 0; i < loopCount; i++) {
      resultArticles = asyncQuery.query(articleCount);
    }
    watch.stop();
    result.add( String.format("time: %d ms , %s", watch.getTotalTimeMillis(), resultArticles));

    return result;
  }
}
